package com.tools.scanner

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.tools.scanner.ui.MainActivity
import com.tools.scanner.ui.dialog.LoadingDialog
import com.tools.scanner.utils.ActivityStack

abstract class BaseActivity : AppCompatActivity() {

    open fun getStatusColor(): Int {
        return -1
    }

    private lateinit var mLoadingDialog: LoadingDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActivityStack.pushActivity(this)
        updateStatusBar()
    }

    private fun updateStatusBar() {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        if (this !is MainActivity) {
            var statusCode = getStatusColor()
            if (statusCode == -1) {
                statusCode = resources.getColor(R.color.color_42a5f5)
            }

            window.statusBarColor = statusCode

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility =
                    (window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv()) or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            } else {
                window.statusBarColor = Color.parseColor("#80000000")
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            }
        } else {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.statusBarColor = Color.TRANSPARENT

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                window.decorView.systemUiVisibility =
                    window.decorView.systemUiVisibility.and(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv())
            } else {
                window.statusBarColor = Color.parseColor("#80000000")
                window.decorView.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            }
        }
    }

    fun showLoadingDialog() {
        if (!this::mLoadingDialog.isInitialized) {
            mLoadingDialog = LoadingDialog(this)
        }

        if (mLoadingDialog.isShowing || isFinishing || isDestroyed) {
            return
        }

        mLoadingDialog.show()
    }

    fun hideLoadingDialog() {
        if (!this::mLoadingDialog.isInitialized) {
            return
        }

        if (mLoadingDialog.isShowing) {
            mLoadingDialog.dismiss()
        }
    }

    override fun onDestroy() {
        ActivityStack.popActivity()
        hideLoadingDialog()
        super.onDestroy()
    }
}